<?php

namespace Home\Controller;

class OrderController extends HomeController {
    //用户订单详情
    public function index($userid = 37){
//    public function index($userid,$token){
        //$this->checkLog($userid,$token);

        $tradeData = M()->table('trade_trade as a')
            ->field('a.evaluatbuyer,a.evaluatseller,a.id,a.order_id,a.type,a.price,a.num,a.addtime,a.adver_id,a.order_status,b.username,b.headimg')
            ->join('left join trade_user as b on a.trade_id = b.id')
            ->where('a.order_status !=0  and a.userid = '.$userid)->select();

        foreach ($tradeData as $k => $v) {
            $tradeData[$k]['symbol'] = 1;
        }

        $Data = M()->table('trade_trade as a')
            ->field('a.evaluatbuyer,a.evaluatseller,a.id,a.order_id,a.type,a.price,a.num,a.addtime,a.adver_id,a.order_status,b.username,b.headimg')
            ->join('left join trade_user as b on a.trade_id = b.id')
            ->where('a.order_status !=0  and a.trade_id = '.$userid)->select();

        foreach ($Data as $k => $v) {
            $Data[$k]['symbol'] = 2;
        }

        $data = array_merge($tradeData,$Data);

        foreach ($data as $k => $v) {
            $chatInfo = M('Chat')->where(array(
                'chatid' => $userid,
                'status' => 0,
                'trade_id' => $v['id']
            ))->getField('id');

            if ($chatInfo){
                $data[$k]['chatlog'] = 1;
            }else{
                $data[$k]['chatlog'] = 0;
            }
        }

        $this->ajaxReturn($data,'JSON');
    }

    //已完成的订单
    public function completeOrder($userid){

        //$this->checkLog($userid,$token);
//        $user = M('User')->where(array(
//            'id' => $userid
//        ))->find();

//        $tradeData = M()->table('trade_trade as a')
//            ->field('a.id,a.order_id,a.type,a.price,a.num,a.addtime,a.order_status,b.username,b.headimg')
//            ->join('left join trade_user as b on a.trade_id = b.id')
//            ->where('order_status >= 3 and userid = '.$userid)->select();


        $tradeData = M()->table('trade_trade as a')
            ->field('a.evaluatbuyer,a.evaluatseller,a.id,a.order_id,a.type,a.price,a.num,a.addtime,a.order_status,b.username,b.headimg')
            ->join('left join trade_user as b on a.trade_id = b.id')
            ->where('a.order_status >=3  and a.userid = '.$userid)->select();

        foreach ($tradeData as $k => $v) {
            $tradeData[$k]['symbol'] = 1;
        }

        $Data = M()->table('trade_trade as a')
            ->field('a.evaluatbuyer,a.evaluatseller,a.id,a.order_id,a.type,a.price,a.num,a.addtime,a.order_status,b.username,b.headimg')
            ->join('left join trade_user as b on a.trade_id = b.id')
            ->where('a.order_status >= 3  and a.trade_id = '.$userid)->select();

        foreach ($Data as $k => $v) {
            $Data[$k]['symbol'] = 2;
        }
        $data = array_merge($tradeData,$Data);
        $this->ajaxReturn($data,'JSON');
    }
}